package com.rock.securityresearch.common.equity.financereport

import com.rock.securityresearch.equity.QuotePriceCsvParser.parseHistoricalStockQuote
import com.rock.securityresearch.equity.HistoricalStockQuote
import com.rock.securityresearch.util.CsvUtil

import java.io.ByteArrayInputStream

object FinanceReportCsvParser {
    def parse(data: Array[Byte], encoding: String): List[FinanceReportItem] = {
        val csvData: List[List[String]] = CsvUtil.read(ByteArrayInputStream(data), encoding)
        val dates: List[String] = csvData.head.tail
        val dataBody = csvData.tail
        val reportItems = dataBody.map{ cols =>
            val name = cols.head
            val values = cols.tail
            val reportItemValues = values.zipWithIndex.map { v => 
                val date = dates(v._2)
                val value = if(v._1 == null || v._1.isBlank) then None else v._1.toDoubleOption
                FinanceReportValue(date, value)
            }
            FinanceReportItem(name, reportItemValues)
        }
        reportItems
    }
}
